searchId = 0;

$(document).ready(function() {
	search();
	$(".group-number").hide();
	$("#show-basic").toggle();
	$(".hidden-form").toggle();
});

$(document).delegate('#group', 'change', function() {
	if ($(this).val()==-2) {
		$(".group-number").show();
	} else {
		$(".group-number").hide();
	}
});

query = "a";

$(document).delegate('.search', 'keyup', function() {
	if ($(".search").val()!= query) {
		query = $(".search").val();
		search();
	}
});

$(document).delegate('#group', 'change', function() {
	if (($(this).val() == "-2") && ($(".group-number").val() == "")) {
		$("#search-error").html("select group number");
		return;
	}
	search();
});

$(document).delegate('#group-number', 'keyup', function() {
	if ($(this).val() != "") {
		search();
	}
});

$(document).delegate('.show-full', 'change', function() {
	search();	
});

$(document).delegate('#sign', 'keyup', function() {
	search();
});

$(document).delegate('#credits', 'keyup', function() {
	search();
});


function search() {
	searchId += 1;
	thisSearchId = searchId;
	data = $("#search-form").serialize();
	$.ajax({
		type:"POST",
		url:'/snayishandsteel88/Courses',
		async: true,
		data: data,
		success:function(msg) {
			if (searchId==thisSearchId) {
				$("#search-table").html(msg);
				$("#search-error").html("");
			}			
		},
		error:function(msg) {
			$("#search-error").html(msg.responseText);
		}
	});
}

function toggleAdvanced() {
	$("#show-advanced").toggle();
	$("#show-basic").toggle();
	$(".hidden-form").toggle();
}

